<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model\Admin;

use App\Model\Model;
use Carbon\Carbon;
use Hyperf\Database\Model\Relations\HasMany;

/**
 * @property int $id 主键
 * @property string $title 标题
 * @property string $icon 图标
 * @property string $key 标识
 * @property int $pid 上级菜单
 * @property Carbon $created_at 创建时间
 * @property Carbon $updated_at 更新时间
 * @property string $href url
 * @property int $type 类型
 * @property int $weight 排序
 * @property int $created_by 添加人
 * @property int $updated_by 修改人
 * @property int $status 状态: 0.启用 1.禁用
 */
class Rule extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'rules';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'title', 'icon', 'key', 'pid', 'created_at', 'updated_at', 'href', 'type', 'weight', 'created_by', 'updated_by', 'status'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'pid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'type' => 'integer', 'weight' => 'integer', 'created_by' => 'integer', 'updated_by' => 'integer', 'status' => 'integer'];

    /**
     * 获取子列表.
     */
    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'pid', 'id')->select(['id', 'pid', 'title']);
    }
}
